package ru.usu.gv.gui.analysis;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.gui.layout.SimpleGridLayout;
import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.uicomponents.UIGraph;
import ru.usu.gv.uicomponents.UIVertex;

import javax.vecmath.Point3d;
import javax.vecmath.Point3i;

import java.util.List;

/**
 @author Sergey Pupyrev 
 13.04.2007
 */

public class VolumeAnalyzer extends GraphAnalyzer
{
    public VolumeAnalyzer()
    {
        super(AnalysisPanel.getInstance().getVolumeSlider());
    }

    @Override
    protected double calculateAnalysValue()
    {
        UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();

        double bestVolume, currentVolume;
        List<UIVertex> vertexList = uiGraph.getVertexes();
        Point3i vol = SimpleGridLayout.calculateGridSize(vertexList.size());
        bestVolume = (double)(vol.x - 1) * (vol.y - 1) * (vol.z - 1);

        double mx = 0.1, my = 0.1, mz = 0.1;
        for (int i = 0; i < vertexList.size(); i++)
            for (int j = i + 1; j < vertexList.size(); j++)
            {
                Point3d posi = vertexList.get(i).getPosition();
                Point3d posj = vertexList.get(j).getPosition();
                double edgeLength = StyleFacade.getEdgeLength();
                double lenx = Math.abs(posi.getX() - posj.getX()) / edgeLength;
                double leny = Math.abs(posi.getY() - posj.getY()) / edgeLength;
                double lenz = Math.abs(posi.getZ() - posj.getZ()) / edgeLength;

                mx = Math.max(lenx, mx);
                my = Math.max(leny, my);
                mz = Math.max(lenz, mz);
            }

        currentVolume = mx * my * mz;
		//        System.out.println("mx " + mx);
		//        System.out.println("bestVolume " + bestVolume);
		//        System.out.println("currentVolume " + currentVolume);
        return bestVolume / currentVolume;
    }

}
